/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.sensor.cropcircle.Impl;

import java.util.List;
import javax.persistence.Query;
import nl.cloudfarming.client.model.CommonModelService;
import nl.cloudfarming.client.model.SensorDataFile;
import nl.cloudfarming.client.model.SensorMetadata;
import nl.cloudfarming.client.sensor.cropcircle.CropcircleModule;
import nl.cloudfarming.client.sensor.cropcircle.model.CropcircleSensorData;

/**
 *
 * @author Merijn Zengers
 */
public class CropcircleServiceImpl extends CommonModelService {

    @Override
    protected String getModuleName() {
        return CropcircleModule.MODULE_NAME;
    }

    public void persist(List<CropcircleSensorData> sensorDataList) {
        startTransaction();
        for (CropcircleSensorData sensorData : sensorDataList) {
            mergeSensorData(sensorData);
        }
        commit();
    }

    public void delete(CropcircleSensorData toBeRemoved) {
        getEntityManager().remove(toBeRemoved);
    }

    public CropcircleSensorData findSensorDataById(long id) {
        startTransaction();
        return getEntityManager().find(CropcircleSensorData.class, id);
    }

    public List<CropcircleSensorData> findAllCropcircleSensorData() {
        startTransaction();
        Query query = this.getEntityManager().createQuery("select c from CropcircleSensorData c");
        return query.getResultList();
    }

    public void updateSensorMetadata(SensorDataFile dataFile, SensorMetadata newSensorMetadata) {
        startTransaction();
        if (dataFile.getId() == 0) {
            throw new IllegalArgumentException("Cannot update a SensorDataFile if it does not exist");
        } else {
            if (newSensorMetadata.getId() != 0) {
                dataFile.setMetadata(newSensorMetadata);
            } else {
                SensorMetadata existing = findMetaDataByMetadata(newSensorMetadata.getDateFormat(), newSensorMetadata.getSensorHeight(), newSensorMetadata.getNumberOfSensors(), newSensorMetadata.getSensorSpread(), newSensorMetadata.getSensorType(), newSensorMetadata.getSpectralChannels());
                if (existing != null) {
                    dataFile.setMetadata(existing);
                    dataFile = getEntityManager().merge(dataFile);
                } else {
                    getEntityManager().persist(newSensorMetadata);
                    dataFile.setMetadata(newSensorMetadata);
                }
            }
            commit();
        }
    }
}
